<?php

namespace app\api\controller;

use app\admin\model\Admin;
use app\api\model\AccountType;
use app\api\model\SetAccount;
use app\api\model\SupplierModel;
use app\api\service\SupplierReconciliation as Service;
use app\api\validate\SupplierReconciliationValidate as Validate;
use app\http\GetUid;
use think\Request;

class SupplierReconciliation extends Common
{

    public function index(Request $request)
    {
        $supplier = $request->param('supplier', 0);
        $data = array_filter($request->param());
        $where = [];
        if (isset($data['maker'])) {
            $where[] = ['maker' , '=', $data['maker']];
        }
        if (isset($data['set_account'])) {
            $where[] = ['set_account_id' , '=', $data['set_account']];
        }
        if (isset($data['number'])) {
            $where[] = ['business_number' , '=', $data['number']];
        }
        if (isset($data['type'])) {
            $where[] = ['account_type_id' , '=', $data['type']];
        }
        if (isset($data['start_time']) && isset($data['end_time'])) {
            $where[] = ['business_time' , 'BETWEEN', [$data['start_time'],$data['end_time']]];
        }
        $page = $request->param('page', 1);
        $size = $request->param('size', 10);
        $res = \app\api\model\SetAccountFlow::with('supplierReconciliation,purchaseReturn,purchase')
            ->where('customer_id', $supplier)->where('customer_type', 'supplier')->where($where)
            ->cache()->page($page, $size)->paginate($size) ->each(function ($v) {
                $v['maker_name'] = Admin::find($v['maker'])['name'];
                $v['account_type_name'] = AccountType::find($v['account_type_id'])['name'];
                $v['set_account_name'] = SetAccount::find($v['set_account_id'])['name'];
                $v['total'] = Service::getTotal($v);
                $v['payment'] = Service::getPayment($v);
                $v['balance'] = $v['total'] - $v['payment'];
                return $v;
            });
        $info['totals'] = array_sum($res->getCollection()->column('total'));
        $info['payment'] = array_sum($res->getCollection()->column('payment'));
        $info['balance'] = array_sum($res->getCollection()->column('balance'));
        $supplier = SupplierModel::find($data['supplier']);
        $info['customer_id'] = (int)$data['supplier'];
        $info['customer_name'] = $supplier['name'];
        $info['customer_arrears'] = $supplier['arrear'];
        return $this->returnJson(200, 'ok', array_merge($res->toArray(), $info));
    }

    /**
     * @param Request $request
     * @param Validate $validate
     * @return \think\response\Json
     */
    public function store(Request $request, Validate $validate)
    {
        $data = $request->param();
        if (!$validate->check($data)) {
            return $this->returnJson(422, $validate->getError());
        }
        $data['maker'] = GetUid::getUId();
        $data['number'] = 'jxc' . time();
        $res = Service::receipt($data);
        return $res ? $this->returnJson(200, $res) : $this->returnJson(500, $res);
    }

    /**
     * 期初调整
     * @param Request $request
     * @param Validate $validate
     * @return \think\response\Json
     */
    public function adjustment(Request $request, Validate $validate)
    {
        $data = $request->param();
        if (!$validate->scene('adjustment')->check($data)) {
            return $this->returnJson(422, $validate->getError());
        }
        $data['maker'] = GetUid::getUId();
        $data['number'] = 'jxc' . time();
        $res = Service::adjustment($data);
        return $res ? $this->returnJson(200, $res) : $this->returnJson(500, $res);
    }
}
